package com.baizhi.serviceImpl;

import com.baizhi.dao.CategoryDao;
import com.baizhi.dto.PageDTO;
import com.baizhi.dto.TwoCategoryPageDTO;
import com.baizhi.entity.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    private static final Logger log1 = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryDao categoryDao;

    /**
     * 分页查所有一级类别
     *
     * @param pageDTO
     */
    @Override
    public CommonVO queryOnePage(PageDTO pageDTO) {
        Page<Category> page = new Page<>(pageDTO.getPage(),pageDTO.getPageSize());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getLevels,1);
        Page<Category> selectPage = categoryDao.selectPage(page, queryWrapper);
        return new CommonVO(pageDTO.getPage(),selectPage.getTotal(),selectPage.getRecords());
    }

    /**
     * 获取二级类别
     *
     * @param twoCategoryPageDTO
     */
    @Override
    public CommonVO queryTwoCategory(TwoCategoryPageDTO twoCategoryPageDTO) {
        /**
         * 根据传递过来的 categoryId 查询对应的所有二级类别
         */
        IPage<Category> page = new Page<>(twoCategoryPageDTO.getPage(), twoCategoryPageDTO.getPageSize());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getLevels,2)
                .eq(Category::getParentId,twoCategoryPageDTO.getCategoryId());

        IPage<Category> categoryIPage = categoryDao.selectPage(page, queryWrapper);

        return new CommonVO(twoCategoryPageDTO.getPage(), categoryIPage.getTotal(),categoryIPage.getRecords());
    }

    @Override
    public Category queryById(String id) {
        Category category = categoryDao.selectById(id);
        return category;
    }

    /**
     * 根据levels查所有一级类别
     *
     * @param category
     */
    @Override
    public List<Category> queryAllLevels(Category category) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getLevels,category.getLevels());
        List<Category> list = categoryDao.selectList(queryWrapper);
        log1.info("list {}",list);
        return list;
    }

    /**
     * 删除类别
     *
     * @param category
     */
    @Override
    public CommonMessageVO delete(Category category) {
        //判断是否是一级类别
        if (category.getParentId()==null){
            //为null是一级类别
            //一级类别id获取对应的parentId
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getParentId,category.getId());
            Integer count = categoryDao.selectCount(queryWrapper);
            log1.info("count {}",count);
            if (count==0){
                //说明该一级类别下没有二级类别
                categoryDao.deleteById(category.getId());
                return CommonMessageVO.succeed();
            }else {
                //删除失败
               return CommonMessageVO.failed();
            }
        }else {
            //删除
            categoryDao.deleteById(category.getId());
            return CommonMessageVO.succeed("删除二级类别成功");
        }
    }

    /**
     * 修改类别名
     *
     * @param category
     */
    @Override
    public CommonMessageVO update(Category category) {
        try {
            int update = categoryDao.updateById(category);
            //int a = 10/0;
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    /**
     * 添加一级类别
     *
     * @param category
     */
    @Override
    public CommonMessageVO add(Category category) {
        try {
            if (category.getParentId()!=null){
                category.setLevels(2);
                int insert = categoryDao.insert(category);
                return CommonMessageVO.succeed("添加二级类别成功");
            }else{
                category.setLevels(1);
                int insert = categoryDao.insert(category);
                return CommonMessageVO.succeed("添加一级类别成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }


}